package com.sdleyou.treepro.business.api.company;

import com.sdleyou.treepro.common.model.dto.CompanyDTO;
import com.sdleyou.treepro.common.model.utils.ResultVOUtil;
import com.sdleyou.treepro.common.model.vo.ResultVO;
import com.sdleyou.treepro.common.service.UserCompanyManager;
import com.sdleyou.treepro.system.version.ApiVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("/api")
public class CompanyController {
    @Autowired
    private UserCompanyManager userCompanyManager;

    /**
     * 发起公司申请
     *
     * @param companyDTO
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/company/apply")
    public ResultVO applyCompany(@RequestBody CompanyDTO companyDTO) {
        userCompanyManager.initCompany(companyDTO);
        return ResultVOUtil.success();
    }

    /**
     * 获取公司员工信息
     *
     * @param companyId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiVersion(1)
    @GetMapping("/{version}/company/users")
    public ResultVO companyUsers(@RequestParam(name = "company_id") @NotEmpty(message = "公司ID不能为空") String companyId,
                                 @RequestParam(name = "page_size", defaultValue = "5") Integer pageSize,
                                 @RequestParam(name = "page_num", defaultValue = "1") Integer pageNum,
                                 @RequestParam(name = "user_id") @NotEmpty(message = "用户ID不为空") String userId,
                                 @RequestParam(name = "keywords", defaultValue = "") String keywords) {
        return ResultVOUtil.success(userCompanyManager.companyUsers(companyId, userId, pageSize, pageNum, keywords));
    }

    /**
     * 获取公司列表接口
     *
     * @return
     */
    @ApiVersion(1)
    @GetMapping("/{version}/companies")
    public ResultVO companies(@RequestParam(name = "page_size", defaultValue = "5") Integer pageSize,
                              @RequestParam(name = "page_num", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "keywords", defaultValue = "") String keywords) {

        return ResultVOUtil.success(userCompanyManager.getCompanies(pageSize, pageNum, keywords));
    }

    /**
     * 获取公司基本信息
     *
     * @return
     */
    @ApiVersion(1)
    @GetMapping("/{version}/company/{company_id}")
    public ResultVO companyInfo(@PathVariable("company_id") String companyId,
                                @RequestParam("user_id") @NotEmpty(message = "user_id不能为空") String userId) {
        return ResultVOUtil.success(userCompanyManager.getCompany(companyId, userId));
    }

    /**
     * 成员退出企业
     *
     * @param companyId
     * @param userId
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/company/quit")
    public ResultVO quitCompany(@RequestParam("company_id") String companyId,
                                @RequestParam("user_id") String userId) {
        userCompanyManager.quitCompany(companyId, userId);
        return ResultVOUtil.success();
    }

    @ApiVersion(1)
    @PostMapping("/{version}/company/join")
    public ResultVO joinCompany(@RequestParam("user_id") @NotEmpty(message = "user_id不能为空") String userId,
                                @RequestParam("company_id") @NotEmpty(message = "company_id不能为空") String companyId) {
        userCompanyManager.joinCompany(companyId, userId);
        return ResultVOUtil.success();
    }

    @ApiVersion(1)
    @GetMapping("/{version}/company/applies")
    public ResultVO companyApplies(@RequestParam("company_id") @NotEmpty(message = "company_id不能为空") String companyId,
                                   @RequestParam(name = "page_size", defaultValue = "5") Integer pageSize,
                                   @RequestParam(name = "page_num", defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "status", defaultValue = "APPLY") String status) {
        return ResultVOUtil.success(userCompanyManager.companyApplies(companyId, status, pageSize, pageNum));
    }

    @ApiVersion(1)
    @PostMapping("/{version}/company/audit")
    public ResultVO auditApply(@RequestParam("company_id") @NotEmpty(message = "company_id不能为空") String companyId,
                               @RequestParam(name = "user_id") @NotEmpty(message = "user_id不能为空") String userId,
                               @RequestParam(name = "status") @NotEmpty(message = "审核记过不能为空") String status) {
        userCompanyManager.auditApply(companyId, userId, status);
        return ResultVOUtil.success();
    }


}
